package com.wgts.wgts.mvp.view.activity.myorder;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.dialog.CancelListDate;
import com.wgts.wgts.control.dialog.CancleListDialogFragment;
import com.wgts.wgts.control.recycleview.CustomLineaLayoutManager;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerAfterSaleActivityComponent;
import com.wgts.wgts.dagger.module.AfterSaleActivityModule;
import com.wgts.wgts.localbean.GoodsData;
import com.wgts.wgts.mvp.presenter.IAfterSaleActivityPresenter;
import com.wgts.wgts.mvp.view.IAfterSaleActivityView;
import com.wgts.wgts.mvp.view.activity.pay.ContinuePayActivity;
import com.wgts.wgts.netmodle.resp.AfterSaleEntity;
import com.wgts.wgts.tools.MoHuGlide;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *
 *
 *   data:2018/9/2519:02
 *
 */public class AfterSaleActivity extends BaseActivity implements IAfterSaleActivityView {
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.after_sale_recycle)
    RecyclerView afterSaleRecycle;
    @BindView(R.id.show_or_hidden_text)
    TextView showOrHiddenText;
    @BindView(R.id.show_or_hidden_image)
    ImageView showOrHiddenImage;
    @BindView(R.id.show_or_hidden_recycle)
    LinearLayout showOrHiddenRecycle;
    @BindView(R.id.after_sale_question_text)
    TextView afterSaleQuestionText;
    @BindView(R.id.after_sale_question_ly)
    LinearLayout afterSaleQuestionLy;
    @BindView(R.id.exchange_goods_image)
    ImageView exchangeGoodsImage;
    @BindView(R.id.exchange_goods_ly)
    LinearLayout exchangeGoodsLy;
    @BindView(R.id.back_goods_image)
    ImageView backGoodsImage;
    @BindView(R.id.back_goods_ly)
    LinearLayout backGoodsLy;
    @BindView(R.id.back_goods_message_et)
    EditText backGoodsMessageEt;
    @BindView(R.id.submit_apply_for)
    LinearLayout submitApplyFor;
    @BindView(R.id.bottom_ly)
    LinearLayout bottomLy;
    @BindView(R.id.img_product)
    ImageView imgProduct;
    @BindView(R.id.product_name)
    TextView productName;
    @BindView(R.id.order_all_amount)
    TextView orderAllAmount;
    @BindView(R.id.order_all_price)
    TextView orderAllPrice;
    @BindView(R.id.wait_pay_ly)
    RelativeLayout waitPayLy;
    @BindView(R.id.shopping_car_type_show)
    TextView shoppingCarTypeShow;

    @Inject
    IAfterSaleActivityPresenter presenter;

    private AfterSaleActivityAdapter adapter;
    private boolean showAll = false;
    private CancelListDate cancelListDate;
    private List<AfterSaleEntity.Product> productList;
    //服务类型 0 换货， 1退货换货
    private String chooseType = "EXCHANGE";
    public static final String ORDER_ID = "orderId";
    public static final String ORDER_CODE = "orderCode";
    public static final String GOODS_DATE = "goodsDate";
    private Integer orderId;
    private String orderCode;
    private String changeQuestion;
    private GoodsData goodsData;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.after_sale_activity);
        ButterKnife.bind(this);
        includeTitle.setText("申请售后");
        Intent intent = getIntent();
        orderCode = intent.getStringExtra(ORDER_CODE);
        if (intent.getStringExtra(ORDER_ID) != null && !intent.getStringExtra(ORDER_ID).equals("")) {
            orderId = Integer.parseInt(intent.getStringExtra(ORDER_ID));
        }
        initView();
    }

    @Override
    public void init() {
        DaggerAfterSaleActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .afterSaleActivityModule(new AfterSaleActivityModule(this))
                .build()
                .inject(this);
        productList = new ArrayList<>();
    }

    private void initView() {
        goodsData = getIntent().getParcelableExtra(GOODS_DATE);
        MoHuGlide.getInstance().glideImgsFormList(imgProduct, goodsData.getUrl(), this);
        productName.setText(goodsData.getProductName());
        shoppingCarTypeShow.setText(goodsData.getProductType());
        orderAllPrice.setText(goodsData.getProductPrice());
        CustomLineaLayoutManager lineaLayoutManager = new CustomLineaLayoutManager(this);
        lineaLayoutManager.setScrollEnabled(false);
        afterSaleRecycle.setLayoutManager(lineaLayoutManager);
        //adapter = new AfterSaleActivityAdapter(this, productList);
        afterSaleRecycle.setAdapter(adapter);
        exchangeGoodsImage.setSelected(true);
        presenter.initDate();
    }

    @OnClick({R.id.back_btn_ly, R.id.show_or_hidden_recycle, R.id.after_sale_question_ly, R.id.exchange_goods_ly, R.id.back_goods_ly, R.id.submit_apply_for})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_ly:
                finish();
                break;
            case R.id.show_or_hidden_recycle:
                if (showAll) {
                    showAll = false;
                    presenter.initList(showAll);
                    showOrHiddenText.setText("展开全部商品");
                    showOrHiddenImage.setImageResource(R.mipmap.ic_public_load);
                } else {
                    showAll = true;
                    presenter.initList(showAll);
                    showOrHiddenText.setText("收起全部商品");
                    showOrHiddenImage.setImageResource(R.mipmap.ic_public_up);
                }
                break;
            case R.id.after_sale_question_ly:
                showToast();
                break;
            case R.id.exchange_goods_ly:
                chooseType(0);
                break;
            case R.id.back_goods_ly:
                chooseType(1);
                break;
            case R.id.submit_apply_for:
                submitEvaluate();
                break;
        }
    }

    //展示售后原因弹出框
    private void showToast() {
        final CancleListDialogFragment dialogFragment = CancleListDialogFragment.getCancleListDialogFragment(cancelListDate);
        dialogFragment.show(getSupportFragmentManager(), "CancelListDialog");
        dialogFragment.setOnClickListen(new CancleListDialogFragment.onClickListen() {
            @Override
            public void onClick(String s) {
                afterSaleQuestionText.setText(s);
                changeQuestion = s;
                dialogFragment.dismiss();
            }
        });
    }

    //选择服务类型
    private void chooseType(int type) {
        switch (type) {
            case 0:
                exchangeGoodsImage.setSelected(true);
                backGoodsImage.setSelected(false);
                chooseType = "EXCHANGE";
                break;
            case 1:
                exchangeGoodsImage.setSelected(false);
                backGoodsImage.setSelected(true);
                chooseType = "RETURN";
                break;
        }
    }

    //提交申请方法
    private void submitEvaluate() {
        if (changeQuestion == null || changeQuestion.equals("") ||
                backGoodsMessageEt.getText().toString().trim() == null || backGoodsMessageEt.getText().toString().trim().equals("")) {
            MoHuToast.showToast("请保持信息完整");
            return;
        }
        showDialog();
        presenter.issueAfterSale(orderId, changeQuestion, chooseType, backGoodsMessageEt.getText().toString().trim(), "", orderCode);
    }

    @Override
    public void onError(int status, String msg) {
        dialogDismiss();
        MoHuToast.showToast(msg);
    }

    @Override
    public void initDate(List<String> reasons) {
        cancelListDate = new CancelListDate();
        cancelListDate.setList(reasons);
        cancelListDate.setTitle("请选择售后原因");
    }

    @Override
    public void initList(List<AfterSaleEntity.Shop> entities) {
        CustomLineaLayoutManager lineaLayoutManager = new CustomLineaLayoutManager(this);
        lineaLayoutManager.setScrollEnabled(false);
        afterSaleRecycle.setLayoutManager(lineaLayoutManager);
        adapter = new AfterSaleActivityAdapter(this, entities);
        afterSaleRecycle.setAdapter(adapter);
    }

    @Override
    public void issueAfterSale() {
        dialogDismiss();
        Intent intent = new Intent();
        intent.putExtra("PayStatue", "success");
        setResult(ContinuePayActivity.PAY_RESULT_OK, intent);
        finish();
    }


}
